//
//  HEDoctorInfoModel.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/11/16.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEDoctorInfoModel.h"
#import <RZColorful/RZColorful.h>

@implementation HEDoctorInfoModel

- (instancetype)init {
    if (self = [super init]) {
        [HEGoodsModel mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
            return @{ @"desc" : @"description" };
        }];
    }

    return self;
}

- (BOOL)privateExpire {
    if ([DYTools isBlankString:_user_doc_flag]) {
        _privateExpire = YES;
        return _privateExpire;
    }
    _privateExpire = ![@"1" isEqualToString:_user_doc_flag];
    return _privateExpire;
}

- (NSString *)KDeptStr {
    if (!_KDeptStr) {
        _KDeptStr = [NSString stringWithFormat:@"%@ | %@", _post_name, _depart_name];
    }
    return _KDeptStr;
}

- (NSString *)KSkilful {
    if (!_KSkilful) {
        _KSkilful = [NSString stringWithFormat:@"擅长：%@", _skilful];
    }
    return _KSkilful;
}

- (NSString *)KTagName {
    if (!_KTagName) {
        if ([@"1" isEqualToString:_user_doc_flag]) {
            _KTagName = @"我的医生";
        }else if ([@"2" isEqualToString:_user_doc_flag]) {
            _KTagName = @"已关注";
        }else {
            _KTagName = @"";
        }
    }
    return _KTagName;
}

- (UIColor *)KTagColor {
    if (!_KTagColor) {
        if ([@"1" isEqualToString:_user_doc_flag]) {
            _KTagColor = KHexColor(@"F2791C");
        }else if ([@"2" isEqualToString:_user_doc_flag]) {
            _KTagColor = CNavBgColor;
        }else {
            _KTagColor = KWhiteColor;
        }
    }
    return _KTagColor;
}

- (NSString *)KRatio {
    if (!_KRatio) {
        NSString *app = [DYTools isBlankString:_approval] ? @"0":_approval;
        NSString *satif = [DYTools isBlankString:_satisfaction] ? @"0":_satisfaction;

        _KRatio = [NSString stringWithFormat:@"接诊率:%@%%  好评率:%@%%", app, satif];
    }
    return _KRatio;
}

- (NSString *)KapprovalRate {
    if (!_KapprovalRate) {
        NSString *app = [DYTools isBlankString:_approval] ? @"0":_approval;
        _KapprovalRate = [NSString stringWithFormat:@"%@%%", app];
    }
    return _KapprovalRate;
}

- (NSString *)KsatifRate {
    if (!_KsatifRate) {
        NSString *satif = [DYTools isBlankString:_satisfaction] ? @"0":_satisfaction;
        _KsatifRate = [NSString stringWithFormat:@"%@%%", satif];
    }
    return _KsatifRate;
}


- (NSString *)Kmin_price {
    if (!_Kmin_price) {
        if ([DYTools isBlankString:_min_price]) {
            _Kmin_price = [NSString stringWithFormat:@"￥%@起", @"0"];
        }else {
            _Kmin_price = [NSString stringWithFormat:@"￥%@起", _min_price];
        }
    }
    return _Kmin_price;
}

- (NSAttributedString *)KdetailHtml {
    if (!_KdetailHtml) {
        
        NSMutableAttributedString *attr = [NSAttributedString htmlString: _detail_mobile_html].mutableCopy;

        // 图片的尺寸，在转换保存的时候，是保存的图片本身的size（px），在转换为富文本的时候，并没有转为pt，所以会有可能变大并别裁剪，这里做一个尺寸适配
        NSArray <RZAttributedStringInfo *> *array = [attr rz_attributedStringByAttributeName:NSAttachmentAttributeName];
        [array enumerateObjectsUsingBlock:^(RZAttributedStringInfo * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSTextAttachment *att = obj.value;
            UIImage *image = att.image;
            if (image == nil) {
                image = [UIImage imageWithData:att.fileWrapper.regularFileContents];
            }
            CGFloat width = MIN(image.size.width, KScreenWidth - 20);
            CGFloat height = MIN(image.size.height, image.size.height * width / image.size.width);
            CGRect bounds = att.bounds;
            bounds.size = CGSizeMake(width, height);
            NSAttributedString *temp = [NSAttributedString rz_colorfulConfer:^(RZColorfulConferrer *confer) {
                confer.image(image).bounds(bounds);
            }];
            [attr replaceCharactersInRange:obj.range withAttributedString:temp];
        }];
        [attr addAttributes:@{NSFontAttributeName: DYFont(15)} range:NSMakeRange(0, attr.length)];

        
//        NSMutableAttributedString *htmlString = [[NSMutableAttributedString alloc] initWithData:[self.detailMobileHtml dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute:[NSNumber numberWithInt:NSUTF8StringEncoding]} documentAttributes:nil error:nil];;
        _KdetailHtml = attr;
    }
    return _KdetailHtml;
}


- (CGFloat)KPriceWidth {
    if (!_KPriceWidth) {
        _KPriceWidth = [self.Kmin_price widthForFont:DYBlodFont(15)] + 20;
    }
    return _KPriceWidth;
}

- (CGFloat)KTagWidth {
    if (!_KTagWidth) {
        _KTagWidth = [self.KTagName widthForFont:DYFont(13)] + 10;
    }
    return _KTagWidth;
}

- (NSString *)KUserDocState {
    if (!_KUserDocState) {
        _KUserDocState = [@"1" isEqualToString:_user_doc_flag] ? @"使用中":@"已过期"; 
    }
    return _KUserDocState;
}

- (UIColor *)KUserDocStateColor {
    if (!_KUserDocStateColor) {
        _KUserDocStateColor = [@"1" isEqualToString:_user_doc_flag] ? CNavBgColor:CNavDargGrayColor;
    }
    return _KUserDocStateColor;
}

- (NSString *)KStateText {
    if (!_KStateText) {
        if ([@"3" isEqualToString: _status] ||
            [@"5" isEqualToString: _status]) {
            _KStateText = @"已转诊";
            _KStateTextColor = CPriceColor;
        }else if ([@"0" isEqualToString: _status] ||
                  [@"1" isEqualToString: _status]) {
            _KStateText = @"进行中";
            _KStateTextColor = CNavBgColor;
        }else if ([@"2" isEqualToString: _status]) {
            _KStateText = @"已结束";
            _KStateTextColor = CNavDargGrayColor;
        }
    }
    return _KStateText;
}

@end
